package com.zysf.library_boot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zysf.library_boot.dto.LendDto;
import com.zysf.library_boot.entity.Admin;
import com.zysf.library_boot.entity.BookInfo;
import com.zysf.library_boot.entity.LendList;
import com.zysf.library_boot.entity.ReaderInfo;
import com.zysf.library_boot.mapper.BookInfoMapper;
import com.zysf.library_boot.mapper.ReaderInfoMapper;
import com.zysf.library_boot.service.LendListService;
import com.zysf.library_boot.util.Result;
import org.apache.catalina.connector.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/lendList")
public class LendListController {
    @Autowired
    LendListService lendListService;

    @Autowired
    ReaderInfoMapper readerInfoMapper;

    @Autowired
    BookInfoMapper bookInfoMapper;

    @RequestMapping("/getCustomLendList")
    public Result getCustomLendList(@RequestParam(required = false) String readernumber,
                                    @RequestParam(required = false) String name,
                                    @RequestParam(required = false) Integer backtype,
                                    @RequestParam int currentPage,
                                    @RequestParam int pageSize) {
        Page<LendDto> page = new Page<>(currentPage, pageSize);
        IPage<LendDto> lendListIPage = lendListService.getCustomLendList(page, readernumber, name, backtype);

        Map<String, Object> result = new HashMap<>();
        result.put("records", lendListIPage.getRecords());
        result.put("total", lendListIPage.getTotal());

        return Result.ok(result);
    }

    @PostMapping("/addLendList")
    public Result addLendList(@RequestBody LendList lendList) {
        boolean success = lendListService.addLendList(lendList);
        return success ? Result.ok("添加成功") : Result.fail("添加失败");
    }

    @RequestMapping("/deleteLendListById")
    public Result deleteLendListById(Integer id){
        int num = lendListService.deleteLendListById(id);
        return num > 0 ? Result.ok() : Result.fail("删除失败");
    }

    @RequestMapping("/deleteById")
    public Result deleteById(Integer id){
        boolean l = lendListService.removeById(id);
        return Result.ok(l);
    }

    @RequestMapping("/batchDeleteLendList")
    public Result batchDeleteLendList(@RequestBody List<Integer> ids) {
        boolean m = lendListService.removeByIds(ids);
        return Result.ok(m);
    }

    @PostMapping("/updateLendList")
    public Result updateLendList(@RequestBody LendList lendList) {
        boolean updated = lendListService.updateById(lendList);
        if (updated) {
            return Result.ok("修改成功");
        } else {
            return Result.fail("修改失败");
        }
    }

    @PostMapping("/addReaderLendList")
    public Result addReaderLendList(@RequestBody LendList lendList) {
        boolean success = lendListService.addLendList(lendList);
        if (success) {
            // 更新读者的借阅数量
            ReaderInfo readerInfo = readerInfoMapper.selectById(lendList.getReaderid());
            if (readerInfo != null) {
                readerInfo.setNumberborrow(readerInfo.getNumberborrow() + 1);
                readerInfoMapper.updateById(readerInfo);
            }

            // 更新图书的借阅总数
            BookInfo bookInfo = bookInfoMapper.selectById(lendList.getBookid());
            if (bookInfo != null) {
                bookInfo.setTotal(bookInfo.getTotal() + 1);
                bookInfoMapper.updateById(bookInfo);
            }
        }
        return success ? Result.ok("添加成功") : Result.fail("添加失败");
    }

    @RequestMapping("/getBorrowStatus")
    public Result getBorrowStatus(@RequestParam Integer bookid, @RequestParam Integer readerid) {
        QueryWrapper<LendList> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("bookid", bookid).eq("readerid", readerid);

        LendList lendList = lendListService.getOne(queryWrapper);
        if (lendList != null) {
            Map<String, Object> result = new HashMap<>();
            result.put("backtype", lendList.getBacktype());
            return Result.ok(result);
        } else {
            return Result.fail("未找到借阅记录");
        }
    }

    @RequestMapping("/BorrowCount")
    public Result BorrowCount(){
        QueryWrapper<LendList> qwBorrow =new QueryWrapper<>();
        qwBorrow.select("CONCAT(YEAR(lendDate),'年') as year","count(*) as count");
        qwBorrow.groupBy("year");
        List<Map<String,Object>> i =lendListService.listMaps(qwBorrow);
        return Result.ok(i);
    }

}
